
/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy-layui
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy-layui
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.cms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.modular.cms.entity.ArticleContent;
import vip.xiaonuo.modular.cms.entity.Catalog;
import vip.xiaonuo.modular.cms.entity.Page;
import vip.xiaonuo.modular.cms.enums.PageExceptionEnum;
import vip.xiaonuo.modular.cms.mapper.PageMapper;
import vip.xiaonuo.modular.cms.param.PageParam;
import vip.xiaonuo.modular.cms.service.ArticleContentService;
import vip.xiaonuo.modular.cms.service.CatalogService;
import vip.xiaonuo.modular.cms.service.PageService;
import vip.xiaonuo.modular.cms.vo.PageVO;

import javax.annotation.Resource;
import java.util.List;

/**
 * 页面service接口实现类
 *
 * @author lbh
 * @date 2022-09-07 15:11:43
 */
@Service
public class PageServiceImpl extends ServiceImpl<PageMapper, Page> implements PageService {

    @Autowired
    CatalogService catalogService;

    @Autowired
    ArticleContentService articleContentService;

    @Override
    public PageResult<PageVO> page(PageParam pageParam) {
        QueryWrapper<Page> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(pageParam)) {

            // 根据标题 查询
            if (ObjectUtil.isNotEmpty(pageParam.getTitle())) {
                queryWrapper.lambda().eq(Page::getTitle, pageParam.getTitle());
            }
            // 根据标签 查询
            if (ObjectUtil.isNotEmpty(pageParam.getTags())) {
                queryWrapper.lambda().eq(Page::getTags, pageParam.getTags());
            }
            // 根据摘要 查询
            if (ObjectUtil.isNotEmpty(pageParam.getDescription())) {
                queryWrapper.lambda().eq(Page::getDescription, pageParam.getDescription());
            }
            // 根据分类ID 查询
            if (ObjectUtil.isNotEmpty(pageParam.getCatalogId())) {
                queryWrapper.lambda().eq(Page::getCatalogId, pageParam.getCatalogId());
            }
            // 根据分类名称 查询
            if (ObjectUtil.isNotEmpty(pageParam.getCatalogName())) {
                queryWrapper.lambda().eq(Page::getCatalogName, pageParam.getCatalogName());
            }
            // 根据关键词 查询
            if (ObjectUtil.isNotEmpty(pageParam.getKeywords())) {
                queryWrapper.lambda().eq(Page::getKeywords, pageParam.getKeywords());
            }
            // 根据发布状态 查询
            if (ObjectUtil.isNotEmpty(pageParam.getStatus())) {
                queryWrapper.lambda().eq(Page::getStatus, pageParam.getStatus());
            }
            // 根据发布时间 查询
            if (ObjectUtil.isNotEmpty(pageParam.getPublishTime())) {
                queryWrapper.lambda().eq(Page::getPublishTime, pageParam.getPublishTime());
            }
            // 根据序号 查询
            if (ObjectUtil.isNotEmpty(pageParam.getOrderNum())) {
                queryWrapper.lambda().eq(Page::getOrderNum, pageParam.getOrderNum());
            }
            // 根据置顶 查询
            if (ObjectUtil.isNotEmpty(pageParam.getIsTop())) {
                queryWrapper.lambda().eq(Page::getIsTop, pageParam.getIsTop());
            }
            // 根据热点 查询
            if (ObjectUtil.isNotEmpty(pageParam.getIsHot())) {
                queryWrapper.lambda().eq(Page::getIsHot, pageParam.getIsHot());
            }
            // 根据图片路径 查询
            if (ObjectUtil.isNotEmpty(pageParam.getImageSrc())) {
                queryWrapper.lambda().eq(Page::getImageSrc, pageParam.getImageSrc());
            }
        }
        return new PageResult<>(this.baseMapper.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<Page> list(PageParam pageParam) {
        return this.list();
    }

    @Override
    public void add(PageParam pageParam) {
        Page page = new Page();
        Long catalogId = pageParam.getCatalogId();
        String name = catalogService.getById(catalogId).getName();
        pageParam.setCatalogName(name);
        BeanUtil.copyProperties(pageParam, page);
        this.save(page);

        ArticleContent articleContent = new ArticleContent();
        articleContent.setArticleId(page.getId());
        articleContent.setType(pageParam.getType());
        articleContent.setContent(pageParam.getContent());
        // 添加文章内容信息
        articleContentService.save(articleContent);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<PageParam> pageParamList) {
        pageParamList.forEach(pageParam -> {
        Page page = this.queryPage(pageParam);
            this.removeById(page.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(PageParam pageParam) {
        Page page = this.queryPage(pageParam);
        if(pageParam.getCatalogId() != null){
            Long catalogId = pageParam.getCatalogId();
            Catalog catalog = catalogService.getById(catalogId);
            pageParam.setCatalogName(catalog.getName());
        }
        BeanUtil.copyProperties(pageParam, page);
        this.updateById(page);

        // 更新文章内容
        ArticleContent articleContent = articleContentService.getOne(new LambdaQueryWrapper<ArticleContent>().eq(ArticleContent::getArticleId, pageParam.getId()));
        articleContent.setContent(pageParam.getContent());
        articleContentService.updateById(articleContent);
    }

    @Override
    public Page detail(PageParam pageParam) {
        return this.queryPage(pageParam);
    }

    /**
     * 获取页面
     *
     * @author lbh
     * @date 2022-09-07 15:11:43
     */
    private Page queryPage(PageParam pageParam) {
        Page page = this.getById(pageParam.getId());
        if (ObjectUtil.isNull(page)) {
            throw new ServiceException(PageExceptionEnum.NOT_EXIST);
        }
        return page;
    }
}
